<template>
  <div class="app-container">
    <help :show-help="showHelp" />
    <div class="filter-container">
      <el-button
        v-waves
        type="primary"
        icon="el-icon-circle-plus-outline"
        class="filter-item"
        size="small"
        @click="handleCreate"
      >添加</el-button>
    </div>
    <el-table
      v-loading="listLoading"
      :data="list"
      :border="false"
      fit
      style="width: 100%;"
    >
      <el-table-column label="ID" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="名称">
        <template slot-scope="{row}">
          <span>{{ row.name }}</span>
        </template>
      </el-table-column>
      <el-table-column label="链接">
        <template slot-scope="{row}">
          <span>{{ row.url }}</span>
        </template>
      </el-table-column>
      <el-table-column
        label="操作"
        width="200"
        fixed="right"
      >
        <template slot-scope="{ row }">
          <el-button type="primary" size="mini" icon="el-icon-edit" @click="handleUpdate(row)">
            编辑
          </el-button>
          <el-button type="danger" size="mini" icon="el-icon-delete" @click="handleDelete(row)">
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      :total="listQuery.total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.pageSize"
      :page-sizes="[5, 10, 20, 50, 100]"
      @pagination="refresh"
    />
  </div>
</template>

<script>
import { parseTime } from '@/utils'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'
import api from '@/api'
import Help from './components/Help'
// import config from '@/config'

const initQuery = {
  // status: config.news.OPEN,
  page: 1,
  pageSize: 20,
  total: 0,
  // sort: '+id'
}

export default {
  name: 'RoleMenuList',
  components: {
    Pagination,
    Help
  },
  directives: { waves },
  // filters: {
  //   timeFilter(time) {
  //     return time ? parseTime(time, '{m}-{d} {h}:{i}') : ''
  //   },
  //   statusText(status) {
  //     return config.news.statusText(+status)
  //   }
  // },
  data() {
    return {
      // 状态
      status: {},
      // 状态
      statusMap: [],
      // 显示帮助提示
      showHelp: true,
      listLoading: false,
      list: [],
    }
  },
  created() {
    this.parseQuery()
  },
  mounted() {
    this.getList()
  },
  methods: {
    /**
     * 获取数据列表
     */
    getList() {
      this.listLoading = true
      api.roleMenu.list({ ...this.listQuery }).then(response => {
        const { data } = response
        this.list = data.list
        this.listQuery.total = +data.totalCount
        this.listLoading = false
      }).finally(() => {
        this.listLoading = false
      })
    },
    /**
     * 新建
     */
    handleCreate() {
      this.$router.push({
        path: '/permission/menu-create'
      })
    },
    /**
     * 修改
     * @param row
     */
    handleUpdate(row) {
      this.$router.push({
        path: `/permission/menu-edit/${row.id}`
      })
    },
    /**
     * 删除
     * @param row
     */
    handleDelete(row) {
      this.$confirm('您确定要删除吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        api.roleMenu.remove({ id: row.id }).then(response => {
          this.$message({
            message: response.message || '删除成功',
            type: 'success'
          })
          // this.list.forEach(x => {
          //   if (x.id === row.id) {
          //     x.status = this.status.DELETE
          //   }
          // })
          // 删除掉
          this.list = this.list.filter(x => +x.id !== +row.id)
          // this.handleFilter()
        })
      }).catch(() => {})
    },
    /**
     * 刷新
     */
    refresh() {
      this.$router.push({
        path: '/permission/menu',
        query: this.listQuery
      })
    },
    /**
     * 过滤(筛选)
     */
    handleFilter() {
      this.refresh()
    },
    /**
     * 处理筛选条件
     */
    parseQuery() {
      const query = Object.assign({}, this.$route.query)
      // let sort = '-id'
      // this.listQuery = Object.assign({}, { ...initQuery, sort })
      this.listQuery = Object.assign({}, { ...initQuery })
      if (Object.keys(query).length > 0) {
        // 注意这里的类型转换
        query.page && (query.page = +query.page)
        query.pageSize && (query.pageSize = +query.pageSize)
        query.total && (query.total = +query.total)
        // query.status && (query.status = +query.status)
        // query.sort && (sort = query.sort)
      }
      // const sortSymbol = sort[0]
      // const sortColumn = sort.slice(1, sort.length)
      // this.defaultSort = {
      //   prop: sortColumn,
      //   order: sortSymbol === '+' ? 'ascending' : 'descending'
      // }
      this.listQuery = { ...this.listQuery, ...query }
      // this.status = config.brand
    },
  },
  /**
   * 刷新
   * @param to
   * @param from
   * @param next
   */
  beforeRouteUpdate(to, from, next) {
    if (to.path === from.path) {
      const newQuery = Object.assign({}, to.query)
      const oldQuery = Object.assign({}, from.query)
      if (JSON.stringify(newQuery) !== JSON.stringify(oldQuery)) {
        this.getList()
      }
    }
    next()
  }
}
</script>
